<?php

namespace App\HttpController\Applet\Bingbing;

use App\Model\PhotoModel;
use App\Utility\Cache;
use App\Utility\Music;
use Carbon\Carbon;

/**
 * @description 相册控制器
 */
class IndexController extends AuthController {
    //列表
    public function index(){
        $PhotoModel = new PhotoModel();
        $data = $PhotoModel->where('name', 'bingbing')->first();
        $index = json_decode($data['index'], true);
        $index['day'] = '今天是' . date('m月d日');
        $index['time'] = $this->formatTime();
        $music = (new Music())->getMusic($data['music']);

        $newsData = [
            'index' => $index,
            'list' => json_decode($data['list'], true),
            'music' => $music
        ];

        return $this->writeJson(0, $newsData);
    }

    public function formatTime()
    {
        $h = date('H');

        if ($h >= 0 && $h < 7) {
            $message = '凌晨好';
        } else if ($h >= 7 && $h < 12) {
            $message = '早上好';
        } else if ($h >= 12 && $h < 13) {
            $message = '中午好';
        } else if ($h >= 13 && $h < 6) {
            $message = '下午好';
        } else {
            $message = '晚上好';
        }

        return $message;
    }

    //收集模板消息的formid
    public function formId(){
        $request = $this->request();
        $openid = trim($request->getRequestParam('openid')) ?: '';
        $formid = trim($request->getRequestParam('formid')) ?: '';
        if($formid =='the formId is a mock one'){
            return $this->writeJson(1,null,'测试数据');
        }
        if(!$openid || !$formid){
            return $this->writeJson(1,null,'参数错误');
        }
        $queue = new Cache();
        $queue->onQueue('formid');
        $queue->push([
            'openid' => $openid,
            'expire' => Carbon::now()->addDays(7)->timestamp,
            'formid' => $formid,
            'name'   => $this->applet
        ]);
        unset($queue);
        return $this->writeJson(0);
    }


}
